#include <stdio.h>
#include "mini2440InboxForm.h"
#include "ui_mini2440InboxForm.h"
#include "messagesContent.h"
#include "QtGui"

mini2440InboxForm::mini2440InboxForm(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::mini2440InboxForm)
{
    ui->setupUi(this);
    this->hide();
    find();

}

mini2440InboxForm::~mini2440InboxForm()
{
    delete ui;
}

void mini2440InboxForm::changeEvent(QEvent *e)
{
    QWidget::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;
    default:
        break;
    }
}

void mini2440InboxForm::find()
{
    ui->tableWidget->setRowCount(0);
    QString fileName = "*.msg";
    QString path = "./Mess";

//! [4]
    currentDir = QDir(path);
    QStringList files;
    if (fileName.isEmpty())
        fileName = "*";
    files = currentDir.entryList(QStringList(fileName),
                                 QDir::Files | QDir::NoSymLinks);

    showFiles(files);
    ui->tableWidget->sortItems(0,Qt::DescendingOrder);
}

void mini2440InboxForm::showFiles(const QStringList &files)
{
    FILE* fp = NULL;
    for (int i = 0; i < files.size(); ++i) {        
        QString filetitle = files[i];
        QStringList fileList = filetitle.split("_");
        QString filename = fileList[1];
        QTableWidgetItem *dateItem = new QTableWidgetItem(fileList[0]);
        dateItem->setFlags(dateItem->flags() ^ Qt::ItemIsEditable);
        QTableWidgetItem *nameItem = new QTableWidgetItem(filename.split('.')[0]);
        QByteArray ctemp;
        QFile file(currentDir.absoluteFilePath(filetitle));
        file.open(QIODevice::ReadOnly);
        qint64 offset = file.size()-2;
        file.seek(offset);
        ctemp = file.read(1);
        QString out = QString(ctemp);
        file.close();
        if (ctemp == "n")
            nameItem->setTextColor(QColor(225,0,0,225));
            //nameItem->set
        else
            nameItem->setTextColor(QColor(0,0,0,225));
        nameItem->setFlags(nameItem->flags() ^ Qt::ItemIsEditable);
        int row = ui->tableWidget->rowCount();
        ui->tableWidget->insertRow(row);
        ui->tableWidget->setItem(row, 0, dateItem);
        ui->tableWidget->setItem(row, 1, nameItem);
    }
}

void mini2440InboxForm::on_checkInbox_clicked()
{
    find();
    this->show();
}

void mini2440InboxForm::on_tableWidget_cellDoubleClicked(int row, int /*column*/)
{
    QTableWidgetItem *item = ui->tableWidget->item(row,0);
    QTableWidgetItem *item1 = ui->tableWidget->item(row,1);
    QFile file(currentDir.absoluteFilePath(item->text()+"_"+item1->text()+".msg"));
    if (!file.open(QFile::ReadWrite | QFile::Text)) {
        QMessageBox::warning(this, tr("Application"),
                             tr("Cannot read file %1:\n%2.")
                             .arg(currentDir.absoluteFilePath(item->text()))
                             .arg(file.errorString()));
        return;
    }
    item1->setTextColor(QColor(0,0,0,225));
    QTextStream in(&file);  
    messagesContent form2(this);
    //item1->setTextColor(QColor(0,0,0,225));
    QString subject = item1->text();
    QString content = in.readAll();
    //file.open(QFile::Truncate);
    content.remove(content.length()-2,2);
    form2.setContent(&content, &subject);
    content.append("r\a");
    in.flush();
    in.seek(0);
    in << content;
    form2.exec();
}
